<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('模型名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('模型名称')" :maxlength="50" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('模型标识')" prop="key">
            <el-input v-model="form.key" :placeholder="$t('不可重复')" :maxlength="50" show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('表单类型')" prop="formType">
            <el-select v-model="form.formType" :placeholder="$t('请选择')" filterable @change="getFormList(form.formType)">
              <el-option
                v-for="dict in formTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('绑定表单')" prop="formId">
            <el-select v-model="form.formId" :placeholder="$t('请选择')" filterable>
              <el-option
                v-for="formOption in formList"
                :key="formOption.id"
                :label="$t(formOption.name)"
                :value="formOption.id"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('模型类型')" prop="category">
            <el-select v-model="form.category" filterable :placeholder="$t('请选择')">
              <el-option
                v-for="dict in categoryOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <SaveBtn has-permi="act:model" />
    </div>
  </div>

</template>

<script>
  // 引入和后台交互的JS
  import { saveModel, checkKey } from '@/api/act/model/actModel'
  import { getFormList } from '@/api/act/config/wfApplyForm'
  export default {
    props: {
      actModelData: {
        type: Object,
        default: null
      },
      // 复制模型
      copyModel: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        // 模型类型字典
        categoryOptions: [],
        // 表单类型字典
        formTypeOptions: [],
        // 绑定表单集合
        formList: [],
        form: {},
        // 遮罩层
        loading: false,
        // 表单校验
        rules: {
          name: [
            { required: true, message: this.$t('必填信息'), trigger: 'blur' }
          ],
          key: [
            { required: true, message: this.$t('必填信息'), trigger: 'blur' },
            { validator: this.checkKey, trigger: 'blur' }
          ],
          formType: [
            { required: true, message: this.$t('必填信息'), trigger: 'blur' }
          ],
          formName: [
            { required: true, message: this.$t('必填信息'), trigger: 'blur' }
          ]
        }
      }
    },
    watch: {
      actModelData: {
        handler(val) {
          if (val) {
            this.form = val
            // 如果表单类型选择了值，则初始化表单下拉框
            if (this.form.formType !== undefined) {
              this.getFormList(this.form.formType)
            }
          }
        },
        deep: true,
        immediate: true
      }
    },
    created() {
      // 模型类型字典
      this.getDicts('act_model_type').then(response => {
        this.categoryOptions = response.data
      })
      this.getDicts('form_type').then(response => {
        this.formTypeOptions = response.data
      })
    },
    methods: {
      // 取消按钮
      cancel() {
        this.$parent.$parent.open = false
        this.$parent.$parent.reset()
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.loading = true
            this.form.copyModel = this.copyModel
            saveModel(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
                this.$parent.$parent.open = false
                this.$parent.$parent.getList()
                this.loading = false
              } else {
                this.msgError(response.msg)
                this.loading = false
              }
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.locationError(document)
          }
        })
      },
      // 根据表单类型获取表单
      getFormList: function(formType) {
        getFormList(formType).then(response => {
          if (response.code === 200) {
              this.formList = response.data
          } else {
            this.msgError(response.msg)
          }
        })
      },
      // 流程标识是否重复的校验方法
      async checkKey(rule, value, callback) {
        var checkRes = await checkKey(value, this.form.id, this.copyModel)
        if (checkRes) {
          callback()
        } else {
          callback(new Error(this.$t('流程标识不能重复')))
        }
      }

    }

  }

</script>
